package com.example.administrator.myapplication;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2021/1/2.
 */

public class Jsonpaihang {
    private static Jsonpaihang instance;
    private Jsonpaihang(){

    }
    public static Jsonpaihang getInstance(){
        if (instance == null ){
            instance = new Jsonpaihang();
        }
        return instance;
    }
    private  String read(InputStream in){
        BufferedReader reader = null;
        StringBuilder sb = null;
        String line = null;
        try {
            sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(in));
            while ((line = reader.readLine()) != null){
                sb.append(line);
                sb.append("\n");
            }
        }catch (IOException e){
            e.printStackTrace();
            return  "";
        }finally {
            try {
                if (in != null) in.close();
                if (reader !=null) reader.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }

        return sb.toString();
    }
    public List<paihangInfo> getInfosFromJson(Context context){
        List<paihangInfo> paihangInfos = new ArrayList<>();
        InputStream is = null;
        try {
            is = context.getResources().getAssets().open("paihang.json");
            String json = read(is);
            Gson gson = new Gson();
            Type listType = new TypeToken<List<paihangInfo>>(){

            }.getType();
            List<paihangInfo> infoList = gson.fromJson(json,listType);
            return infoList;
        }catch (Exception e){

        }

        return paihangInfos;
    }
}
